package com.ccrfid.rmc6.resource.open;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.alarm.AlarmRuleEntity;
import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallResultDetailEntity;
import com.ccrfid.rmc6.entity.business.prison.rollcall.PrisonRollcallResultEntity;
import com.ccrfid.rmc6.entity.business.rollcall2.RcRecordDetailEntity;
import com.ccrfid.rmc6.entity.business.rollcall2.RcRecordEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelEntity;
import com.ccrfid.rmc6.entity.poi.PoiLocationEntity;
import com.ccrfid.rmc6.pojo.dto.personnel.PersonnelLocatorDto;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.alarm.AlarmRuleService;
import com.ccrfid.rmc6.service.business.prison.rollcall.PrisonRollcallService;
import com.ccrfid.rmc6.service.business.rollcall2.Rollcall2Service;
import com.ccrfid.rmc6.service.personnel.PersonnelService;
import com.ccrfid.rmc6.service.poi.PoiLocationService;
import com.ccrfid.rmc6.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.List;

@Path("open")
@Produces(MediaType.APPLICATION_JSON)
public class OpenResource {
    @Autowired
    private PersonnelService personnelService;
    @Autowired
    private AlarmRuleService alarmRuleService;
    @Autowired
    private PoiLocationService poiLocationService;
    @Autowired
    private Rollcall2Service rollcall2Service;
    @Autowired
    private PrisonRollcallService rollcallService;

    @GET
    @Path("personnels/all")
    public List<PersonnelEntity> getAllPersonnelsEnabled() {
        return personnelService.getAllPersonnelsEnabled();
    }

    @GET
    @Path("personnel/{personnelSn}/locator-info")
    public PersonnelLocatorDto getPersonnelLocatorInfo(@PathParam("personnelSn") String personnelSn) throws AppException {
        return personnelService.getPersonnelLocatorInfo(personnelSn);
    }

    @GET
    @Path("all-personnel-locator-info")
    public List<PersonnelLocatorDto> getAllPersonnelLocatorInfo() {
        return personnelService.getAllPersonnelLocatorInfo();
    }

    @GET
    @Path("alarm-rules/all")
    public List<AlarmRuleEntity> getAllAlarmRules() {
        return alarmRuleService.getAllAlarmRules();
    }

    @GET
    @Path("locations/{id}")
    public PoiLocationEntity getPoiLocationById(@PathParam("id") Long locationId) {
        PoiLocationEntity poiLocationEntity = poiLocationService.getPoiLocationById(locationId);
        return poiLocationEntity;
    }

    @GET
    @Path("locations/all")
    public List<PoiLocationEntity> getAllLocations() {
        return poiLocationService.getAllLocations();
    }

    //4.1 分页查询点名记录
    @GET
    @Path("rc/records")
    public Page<RcRecordEntity> getRcRecordPage(@QueryParam("startTime") Timestamp startTime,
                                                @QueryParam("stopTime") Timestamp stopTime,
                                                @QueryParam("status") Integer status,
                                                @QueryParam("orgGroupId") Long orgGroupId,
                                                @QueryParam("orgGroupName") String orgGroupName,
                                                @QueryParam("locationGroupId") Long locationGroupId,
                                                @QueryParam("locationGroupName") String locationGroupName,
                                                @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        criteria.add(new Criterion("submitStatus", Criterion.Operator.EQ, 1));
        if (startTime != null) {
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.GTE, startTime));
        }
        if (stopTime != null) {
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.LTE, stopTime));
        }
        if (status != null) {
            criteria.add(new Criterion("status", Criterion.Operator.EQ, status));
        }
        if (orgGroupId != null) {
            criteria.add(new Criterion("orgGroupId", Criterion.Operator.EQ, orgGroupId));
        }
        if (!StringUtils.isEmpty(orgGroupName)) {
            criteria.add(new Criterion("orgGroupName", Criterion.Operator.LIKE, '%' + orgGroupName + '%'));
        }
        if (locationGroupId != null) {
            criteria.add(new Criterion("locationGroupId", Criterion.Operator.EQ, locationGroupId));
        }
        if (!StringUtils.isEmpty(locationGroupName)) {
            criteria.add(new Criterion("locationGroupName", Criterion.Operator.LIKE, '%' + locationGroupName + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcall2Service.getRcRecordPage(criteria, pageRequest);
    }

    //4.4 分页查询某个点名记录详细结果
    @GET
    @Path("rc/records/{recordId}/details")
    public Page<RcRecordDetailEntity> getRcRecordDetailPage(@PathParam("recordId") Long recordId,
                                                            @QueryParam("personnelName") String personnelName,
                                                            @QueryParam("cardNumber") Long cardNumber,
                                                            @QueryParam("rollcallStatus") List<Integer> statusList,
                                                            @QueryParam("remark") String remark,
                                                            @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (recordId != null) {
            criteria.add(new Criterion("recordId", Criterion.Operator.EQ, recordId));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, "%" + personnelName + "%"));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (statusList != null && statusList.size() > 0) {
            criteria.add(new Criterion("rollcallStatus", Criterion.Operator.IN, statusList));
        }
        if (!StringUtils.isEmpty(remark)) {
            criteria.add(new Criterion("remark", Criterion.Operator.LIKE, "%" + remark + "%"));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcall2Service.getRcRecordDetailPage(criteria, pageRequest);
    }

    //分页获取手持机点名记录
    @GET
    @Path("handset-rc-results")
    public Page<PrisonRollcallResultEntity> getRollcallResultList(@QueryParam("dutyDate") Date dutyDate,
                                                                  @QueryParam("taskTime") Time taskTime,
                                                                  @QueryParam("policeSn") String policeSn,
                                                                  @QueryParam("policeName") String policeName,
                                                                  @QueryParam("placeId") String placeId,
                                                                  @QueryParam("placeName") String placeName,
                                                                  @QueryParam("resultStatus") Integer resultStatus,
                                                                  @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (dutyDate != null) {
            java.util.Date startTime = DateUtil.parseDateTimeStr(dutyDate.toString() + " 00:00:00");
            java.util.Date stopTime = DateUtil.parseDateTimeStr(dutyDate.toString() + " 23:59:59");
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.GTE, startTime));
            criteria.add(new Criterion("rollcallTime", Criterion.Operator.LTE, stopTime));
        }
        if (taskTime != null) {
            criteria.add(new Criterion("taskTime", Criterion.Operator.EQ, taskTime));
        }
        if (policeSn != null) {
            criteria.add(new Criterion("policeSn", Criterion.Operator.EQ, policeSn));
        }
        if (policeName != null) {
            criteria.add(new Criterion("policeName", Criterion.Operator.LIKE, '%' + policeName + '%'));
        }
        if (placeId != null) {
            criteria.add(new Criterion("placeId", Criterion.Operator.EQ, placeId));
        }
        if (placeName != null) {
            criteria.add(new Criterion("placeName", Criterion.Operator.LIKE, '%' + placeName + '%'));
        }
        if (resultStatus != null) {
            if (resultStatus.equals(0)) {
                criteria.add(new Criterion("attendanceRate", Criterion.Operator.LTE, 0.9999));
            } else if (resultStatus > 0) {
                criteria.add(new Criterion("attendanceRate", Criterion.Operator.GTE, 0.9999));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcallService.getRollcallResultList(criteria, pageRequest);
    }

    //分页获取某个点名记录的详细点名信息
    @GET
    @Path("handset-rc-results/{resultId}/detail")
    public Page<PrisonRollcallResultDetailEntity> getRollcallDetailResult(@PathParam("resultId") Long rollcallResultId,
                                                                          @QueryParam("personnelSn") String personnelSn,
                                                                          @QueryParam("personnelName") String personnelName,
                                                                          @QueryParam("cardNumber") Long cardNumber,
                                                                          @QueryParam("resultStatus") Integer resultStatus,
                                                                          @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (rollcallResultId != null) {
            criteria.add(new Criterion("rollcallResultId", Criterion.Operator.EQ, rollcallResultId));
        }
        if (personnelSn != null) {
            criteria.add(new Criterion("personnelSn", Criterion.Operator.EQ, personnelSn));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (resultStatus != null) {
            if (resultStatus.equals(0)) {
                criteria.add(new Criterion("resultStatus", Criterion.Operator.EQ, 0));
            } else if (resultStatus > 0) {
                criteria.add(new Criterion("resultStatus", Criterion.Operator.GT, 0));
            }
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return rollcallService.getRollcallDetailResult(criteria, pageRequest);
    }
}
